#############################################################################
# Calculate ACME for m=5 multiply imputed data sets.

ACME.1 <- ACME.fun(m.comp.1, y.comp.1)
ACME.2 <- ACME.fun(m.comp.2, y.comp.2)
ACME.3 <- ACME.fun(m.comp.3, y.comp.3)
ACME.4 <- ACME.fun(m.comp.4, y.comp.4)
ACME.5 <- ACME.fun(m.comp.5, y.comp.5)

c.1 <- apply(ACME.1, 2, mean)
c.2 <- apply(ACME.2, 2, mean)
c.3 <- apply(ACME.3, 2, mean)
c.4 <- apply(ACME.4, 2, mean)
c.5 <- apply(ACME.5, 2, mean)

s.1 <- apply(ACME.1, 2, sd)
s.2 <- apply(ACME.2, 2, sd)
s.3 <- apply(ACME.3, 2, sd)
s.4 <- apply(ACME.4, 2, sd)
s.5 <- apply(ACME.5, 2, sd)

coefs <- cbind(c.1, c.2, c.3, c.4, c.5)
c.combined <- apply(coefs, 1, mean)

var.within <- cbind(s.1^2, s.2^2, s.3^2, s.4^2, s.5^2)
var.within <- apply(var.within, 1, mean)

m <- 5
var.between <- (coefs - c.combined)^2
var.between <-  apply(var.between, 1, sum)
var.between <-  (m-1)^-1 * var.between

s.combined <- sqrt(var.within + (1 + m^-1) * var.between )

combined.res <- cbind(c.combined, s.combined)
combined.res <- round(combined.res, 2)

rownames(combined.res) <-c("restrictive", "liberal", "average")

res.ACME <- combined.res[1,1]
res.ACME.se <- combined.res[1,2]

lib.ACME <- combined.res[2,1]
lib.ACME.se <- combined.res[2,2]

av.ACME <- combined.res[3,1]
av.ACME.se <- combined.res[3,2]


